/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.upc.sad.dao;

import java.util.List;
import java.util.ArrayList;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import pe.edu.upc.sad.model.TipoArea;
import pe.edu.upc.sad.util.BError;
import pe.edu.upc.sad.util.Conexion;
import pe.edu.upc.sad.util.ToolSql;


public class TipoAreaDaoImpl implements TipoAreaDao
{

    Connection conn = null;
    private Statement  st   = null;
    private ResultSet  rs   = null;
    private CallableStatement stmt =null;
    private StringBuffer query = null;

    private BError error;

    public TipoAreaDaoImpl()
    {
      
    }
    public void Inicio()
    {
       error = new BError();
       query = new StringBuffer("");
       conn =   Conexion.getConnection();
    }

    public List<TipoArea> getListTipoArea() throws Exception
    {
        List<TipoArea> lstArea = new ArrayList<TipoArea>();
         try{

            //stmt = conn.prepareCall("call USP_SELECT_TIPO_AREA");
            //rs = stmt.getResultSet();
           // rs = stmt.executeQuery();
            Inicio();

            query.append("{ call USP_SELECT_TIPO_AREA() }");
            stmt = conn.prepareCall(query.toString());

            stmt.execute();
            rs = stmt.getResultSet();

            TipoArea area  = null;
            while (rs.next())
            {
//              TipoArea area  = new TipoArea( rs.getInt("IdTipoArea"),
//                                     rs.getString("Descripcion"),
//                                     rs.getInt("Estado")
//                                   );
                area  = new TipoArea();
                area.setDescripcion(rs.getString("Descripcion"));
                area.setEstado( rs.getInt("Estado"));
                area.setIdTipoArea(rs.getInt("IdTipoArea"));
                lstArea.add(area);
            }
           
        }catch (Exception e){
            error.setCodError(100);
            error.setMsgError("Error :" + e.getMessage());

        }finally{
            ToolSql.close(rs, st, stmt, conn);
        }
         return lstArea;
   }

  public int insert (TipoArea area, Connection conn) throws SQLException
  {

   int resultado = 0;

   try
    {
 Inicio();

      query.append("{ call USP_INSERT_TIPOAREA( ?, ?, ? ) }");

      stmt = conn.prepareCall(query.toString());

      stmt.setInt(1,area.getIdTipoArea());
      stmt.setString(2,area.getDescripcion());
      stmt.setInt(3,area.getEstado());

      stmt.execute();

      resultado = 1;//stmt.getInt(32);

    }
    catch(Exception e)
    {
        error.setCodError(100);
        error.setMsgError("Error :" + e.getMessage());
    }
    finally
    {
      ToolSql.close(null,null,stmt,null );
    }
    return resultado;
  }

    @Override
    public TipoArea get(int idArea, Connection conn) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<TipoArea> getListArea() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
